package com.zut.dao.impl;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.Test;

import com.sun.xml.internal.ws.api.addressing.AddressingVersion.EPR;
import com.zut.dao.HouseDao;
import com.zut.entity.House;
import com.zut.utils.DAOConnection;
import com.zut.utils.DBUtils;
import com.zut.utils.JDBCutil;




public class HouseDaoImpl implements HouseDao{

	@Override
	public int getTotalCount() {
		QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
		String sql = "SELECT COUNT(*) from house";
		Object[] params = {};
		int num;
		try {
			num =  queryRunner.query(sql, params, new ScalarHandler<Long>()).intValue();
			return num;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}


	@Override
	public List<House> quaryAll(int m, int n) {
		
		String sql = "select h_area,h_estate,h_unitNumber,h_floor,h_roomNo,h_acreage,h_direction,h_limit,h_price,h_status,h_id\r\n" + 
				"from house\r\n" + 
				"LIMIT "+m+","+n;
		List<House> houses = DAOConnection.dbDQLWithSQL(sql, House.class);
		
		return houses;
	}


	@Override
	public House queryById(int h_id) {
		String sql = "select * from house where h_id=?";
		List<House> list = DAOConnection.dbDQLWithSQL(sql, House.class, h_id);
		 
		return list.get(0);
	}
	@Test
	public void query1() {
		
	}


	@Override
	public void addHouse(House house) {
		Date date = new Date();
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String sql = "insert into house(h_area,h_estate,h_unitNumber,h_floor,h_roomNo,h_acreage,h_direction,h_fitment,h_isDoubleAir,h_limit,h_facility,h_price,h_status,h_img,h_address,h_addtime,h_updateTime) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		DAOConnection.dbDMLWithSQL(sql, house.getH_area(),house.getH_estate(),house.getH_unitNumber(),house.getH_floor(),house.getH_roomNo(),house.getH_acreage(),house.getH_direction(),house.getH_fitment(),house.getH_isDoubleAir(),house.getH_limit(),house.getH_facility(),house.getH_price(),house.getH_status(),house.getH_img(),house.getH_address(),dateFormat.format(date),dateFormat.format(date));
	}
	@Test
	public void test1() {
		Date date = new Date();
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		System.out.println(dateFormat.format(date));
		
	}


	@Override
	public void deleteHouse(int h_id) {
		String sql = "delete from house where h_id=?";
		DAOConnection.dbDMLWithSQL(sql, h_id);
	}


	@Override
	public void updateHouse(House house) {
		
		Date date = new Date();
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String sql = "update house set h_area=?,h_estate=?,h_unitNumber=?,h_floor=?,h_roomNo=?,h_acreage=?,h_direction=?,h_fitment=?,h_isDoubleAir=?,h_limit=?,h_facility=?,h_price=?,h_status=?,h_img=?,h_address=?,h_updateTime=? where h_id=?";
		DAOConnection.dbDMLWithSQL(sql, house.getH_area(),house.getH_estate(),house.getH_unitNumber(),house.getH_floor(),house.getH_roomNo(),house.getH_acreage(),house.getH_direction(),house.getH_fitment(),house.getH_isDoubleAir(),house.getH_limit(),house.getH_facility(),house.getH_price(),house.getH_status(),house.getH_img(),house.getH_address(),dateFormat.format(date),house.getH_id());
		
	}

}
